package com.zjmzxfzhl.modules.purchase.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zjmzxfzhl.common.Result;
import com.zjmzxfzhl.common.base.BaseController;
import com.zjmzxfzhl.common.util.FileUtil;
import com.zjmzxfzhl.modules.purchase.controller.dto.PurchaseOrderDto;
import com.zjmzxfzhl.modules.purchase.controller.dto.PurchaseOrderUpdateDto;
import com.zjmzxfzhl.modules.purchase.controller.dto.PurchaseUpdateUnitPriceDto;
import com.zjmzxfzhl.modules.purchase.controller.vo.PurchaseOrderVo;
import com.zjmzxfzhl.modules.purchase.entity.PurchaseOrder;
import com.zjmzxfzhl.modules.purchase.service.PurchaseOrderService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.File;

/**
 * Controller
 *
 * @author 庄金明
 */
@RestController
@RequestMapping("/purchase/order")
public class PurchaseOrderController extends BaseController {
    @Autowired
    private PurchaseOrderService purchaseOrderService;

    /**
     * 自定义查询列表
     *
     * @param purchaseOrder
     * @param current
     * @param size
     * @return
     */
    @RequiresPermissions("purchase:order:list")
    @GetMapping(value = "/list")
    public Result list(PurchaseOrderDto purchaseOrder, @RequestParam Integer current,
                       @RequestParam Integer size,
                       @RequestParam(required = false) String order,
                       @RequestParam(required = false) Boolean asc) {
        Page page = new Page<PurchaseOrderVo>(current, size);
        if (!StringUtils.isEmpty(order)) {
            if (null == asc || asc) {
                page.addOrder(OrderItem.asc(order));
            } else {
                page.addOrder(OrderItem.desc(order));
            }
        }
        IPage<PurchaseOrderVo> pageList = purchaseOrderService.list(page, purchaseOrder);
        return Result.ok(pageList);
    }

    @RequiresPermissions("purchase:order:list")
    @GetMapping(value = "/queryById")
    public Result queryById(@RequestParam String id) {
        PurchaseOrder purchaseOrder = purchaseOrderService.getById(id);
        return Result.ok(purchaseOrder);
    }

    /**
     * @param purchaseOrderUpdateDto
     * @return
     * @功能：新增
     */
    @RequiresPermissions("purchase:order:save")
    @PostMapping(value = "/save")
    public Result save(@Valid @RequestBody PurchaseOrderUpdateDto purchaseOrderUpdateDto) {
        purchaseOrderService.addOrUpdateOrder(purchaseOrderUpdateDto);
        return Result.ok();
    }

    /**
     * @param purchaseOrderDto
     * @return
     * @功能：修改
     */
    @RequiresPermissions("purchase:order:update")
    @PutMapping(value = "/update")
    public Result update(@Valid @RequestBody PurchaseOrderUpdateDto purchaseOrderDto) {
        purchaseOrderService.addOrUpdateOrder(purchaseOrderDto);
        return Result.ok();
    }

    /**
     * @param ids
     * @return
     * @功能：批量删除
     */
    @RequiresPermissions("purchase:order:delete")
    @DeleteMapping(value = "/delete")
    public Result delete(@RequestParam String ids) {
        if (ids == null || ids.trim().length() == 0) {
            return Result.error("ids can't be empty");
        }
        String[] idsArr = ids.split(",");
        purchaseOrderService.deleteByIds(idsArr);
        return Result.ok();
    }

    /**
     * 导入设备审批单
     * @param multipartFile
     * @return
     */
    @PostMapping(value = "/upload")
    public Result upload(@RequestParam("file") MultipartFile multipartFile) {
        purchaseOrderService.upload(multipartFile);
        return Result.ok();
    }

    /**
     * 导出设备审批单
     *
     * @param orderId
     * @return
     */
    @GetMapping(value = "/export")
    public void export(HttpServletResponse response, @RequestParam("orderId") Long orderId) {
        File exportFile = purchaseOrderService.export(orderId);
        FileUtil.setDownloadResponse(response, exportFile, exportFile.getName(), true);
    }

    @PutMapping(value = "/unitPrice")
    public Result updateUnitPirce(@RequestBody PurchaseUpdateUnitPriceDto dto) {
        purchaseOrderService.updateUnitPrice(dto.getOrderId(), dto.getMaterialId(), dto.getUnitPrice());
        return Result.ok();
    }

}
